package com.shadow.bus.main.work;

import android.app.Activity;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.ProcessEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.tepth.shadow.ui.recycler.RecyclerViewNoBugLinearLayoutManager;
import com.zhangyu.shadow.core.util.toast.ToastUtils;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * Created by zhangyu on 2017/7/13.
 */

public class SelectOperatorDialog extends Dialog {

    public SelectOperatorDialog(@NonNull final Activity context,
                                final TroubleEntity trouble,
                                final OnOperatorListener listener) {
        super(context);
        setTitle(context.getResources().getString(R.string.common_deal_way));
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_select_operator, null);
        TextView tvEmpty = view.findViewById(R.id.tv_empty);
        final RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(context));
        final List<ProcessEntity> list = trouble.getProcessList();
        if (list == null || list.isEmpty()) {
            tvEmpty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            tvEmpty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    ProcessEntity processEntity = (ProcessEntity) adapter.getItem(position);
                    if (listener != null) {
                        listener.onAddProcessOperator(processEntity);
                    }
                }
            });
            recyclerView.setAdapter(new BaseQuickAdapter<ProcessEntity, BaseViewHolder>(
                    R.layout.item_select_operator, list) {
                @Override
                protected void convert(BaseViewHolder helper, ProcessEntity item) {
                    PartEntity part = item.getAccessory() == null ?
                            item.getComponent() : item.getAccessory();
                    String stringBuilder = item.getOperation().getName() +
                            item.getUseCount() +
                            part.getUnit() +
                            item.getName();
                    helper.setText(R.id.tv_des, stringBuilder);
                    helper.setText(R.id.tv_percent, item.getPercentage() + "%");
                }
            });
        }
        view.findViewById(R.id.btn_check).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    // listener.onAlarm();
                    ToastUtils.showToast(getContext(), "无报警信息");
                    //dismiss();
                }
            }
        });
        view.findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddOtherOperator();
                    dismiss();

                }
            }
        });
        setContentView(view);
    }

    interface OnOperatorListener {
        void onAddProcessOperator(ProcessEntity processEntity);

        void onAlarm();

        void onAddOtherOperator();
    }

}
